package com.adlotto2.lcdns.common;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

import com.adlotto2.lcdns.R;
import com.adlotto2.lcdns.LottoAdApplication;
import com.adlotto2.lcdns.activity.LottoAdBlankActivity;
import com.adlotto2.lcdns.activity.ad.LottoAdAllAdListActivity;
import com.adlotto2.lcdns.activity.ad.LottoAdRecAdListActivity;
import com.adlotto2.lcdns.activity.lotto.LottoAdLottoListActivity;
import com.adlotto2.lcdns.activity.mypage.LottoAdMyPageGroupActivity;
import com.adlotto2.lcdns.activity.notify.LottoAdNotifyMainActivity;

/**
 * TabActivity
 * @author Administrator
 *
 */
public class LottoAdTabActivity extends TabActivity{
	private LottoAdApplication mApplication;
	private TabHost mTabHost;
	private Context mContext;
	
	private Resources mRes;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.mContext = this;
		mApplication = (LottoAdApplication) getApplicationContext();
		
		mRes = mContext.getResources();
		
		setContentView(R.layout.lottoad_base_tab_container);
		
		tabComponents();
	}

	private void tabComponents() {
		mTabHost = getTabHost();
		TabHost.TabSpec spec;

		Intent intent;
		
		intent = new Intent().setClass(this, LottoAdBlankActivity.class);
		spec = mTabHost.newTabSpec("blank")
			.setIndicator(tabIndicatorView(mContext, mRes.getDrawable(R.drawable.menu_recevelotto_btn)))
			.setContent(intent);
		mTabHost.addTab(spec);
		
		// LottoActivity
		intent = new Intent().setClass(this, LottoAdLottoListActivity.class);
		intent.putExtra("type", "LottoList");
		spec = mTabHost.newTabSpec("Lotto")
			.setIndicator(tabIndicatorView(mContext, null))
			.setContent(intent);
		mTabHost.addTab(spec);

		// AllAdActivity
		intent = new Intent().setClass(this, LottoAdAllAdListActivity.class);

		spec = mTabHost.newTabSpec("AllAd")
			.setIndicator(tabIndicatorView(mContext, null))
			.setContent(intent);
		mTabHost.addTab(spec);

		// RecAdActivity
		intent = new Intent().setClass(this, LottoAdRecAdListActivity.class);
		intent.putExtra("type", "AdList");
		spec = mTabHost.newTabSpec("RecAd")
			.setIndicator(tabIndicatorView(mContext, null))
			.setContent(intent);
		mTabHost.addTab(spec);

		// MyPageActivity
		intent = new Intent().setClass(this, LottoAdMyPageGroupActivity.class);

		spec = mTabHost.newTabSpec("MyPage")
			.setIndicator(tabIndicatorView(mContext, null))
			.setContent(intent);
		mTabHost.addTab(spec);
		
		// NotifyActivity
		intent = new Intent().setClass(this, LottoAdNotifyMainActivity.class);

		spec = mTabHost.newTabSpec("Notify")
			.setIndicator(tabIndicatorView(mContext, null))
			.setContent(intent);
		mTabHost.addTab(spec);
		
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {			
			@Override
			public void onTabChanged(String tabId) {
				if (tabId.equals("Lotto")) {
					mTabHost.getTabWidget().getChildAt(1).findViewById(R.id.tab_button).setBackgroundDrawable(mRes.getDrawable(R.drawable.menu_recevelotto_btn));
					mTabHost.getTabWidget().getChildAt(2).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(3).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(4).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(5).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					
				} else if (tabId.equals("AllAd")) {
					mTabHost.getTabWidget().getChildAt(1).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(2).findViewById(R.id.tab_button).setBackgroundDrawable(mRes.getDrawable(R.drawable.menu_adlist_btn));
					mTabHost.getTabWidget().getChildAt(3).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(4).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(5).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					
				} else if (tabId.equals("RecAd")) {
					mTabHost.getTabWidget().getChildAt(1).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(2).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(3).findViewById(R.id.tab_button).setBackgroundDrawable(mRes.getDrawable(R.drawable.menu_recevead_btn));
					mTabHost.getTabWidget().getChildAt(4).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(5).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					
				} else if (tabId.equals("MyPage")) {
					mTabHost.getTabWidget().getChildAt(1).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(2).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(3).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(4).findViewById(R.id.tab_button).setBackgroundDrawable(mRes.getDrawable(R.drawable.menu_mypage_btn));
					mTabHost.getTabWidget().getChildAt(5).findViewById(R.id.tab_button).setBackgroundDrawable(null);
				
				} else if (tabId.equals("Notify")) {
					mTabHost.getTabWidget().getChildAt(1).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(2).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(3).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(4).findViewById(R.id.tab_button).setBackgroundDrawable(null);
					mTabHost.getTabWidget().getChildAt(5).findViewById(R.id.tab_button).setBackgroundDrawable(mRes.getDrawable(R.drawable.menu_info_btn));
				} 
			}
		});
		
		mTabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);
		mTabHost.setCurrentTab(((Activity)mContext).getIntent().getIntExtra("currentTab", 0));
	}
	
	/**
	 * 탭 인디케이터
	 */
	private View tabIndicatorView(Context context, Drawable drawable) {
		View view = LayoutInflater.from(context).inflate(R.layout.lottoad_common_bottom_tab, null);
		ImageView tab_button = (ImageView) view.findViewById(R.id.tab_button);
		tab_button.setBackgroundDrawable(drawable);

		return view;
	}
}
